import {cbs} from '../../../../config/settings.js';
import Upload from '../../../Common/Upload/';
import ScreenShot from '../../../Common/ScreenShot/'

let pos = {};
let geo;
let searchDom = null;

module.exports = {
	name: 'edit',
	data(){
		let hasPos = (rule, value, cb) => {
			if(!value) {
				if(!pos.lng) {
					cbs.submitError.call(this, `请选择经纬度`);
				}else {
					this.basicForm.longitude = pos.lng;
					this.basicForm.latitude = pos.lat;
				}
			}else {
				cb()
			}
		};

	  	return{
	  		basicForm: {
	  			detailAddress: '',
	  			latitude: '',
	  			longitude: '',
	  			coverPhotoUrl: '',
	  			detailPhotoUrl: '',
	  			address: ''
	  		},
	  		basicRules: {
	  			detailAddress: [{
					validator: (rule, value, cb) => {
						if(value === '') {
							cb(new Error('请输入详细地址'));
						}else {
							cb()
						}
					},
					trigger: 'blur'
				}],
	  			longitude: [{
					validator: hasPos,
					trigger: 'blur'
				}],
				latitude: [{
					validator: hasPos,
					trigger: 'blur'
				}],
	  		},
	  		markers: [
				[113.256874, 23.18562]
			],
			center: [113.256874, 23.18562],
			zoom: 12,
			events: {
				init(o) {
					geo = new AMap.Geocoder({
						radius: 1000,
						extensions: 'all'
					})
				},
				click(e) {
					pos = e.lnglat;
					geo.getAddress(pos, (status, result) => {
						if (status === 'complete' && result.info === 'OK') {
							searchDom.val(result.regeocode.formattedAddress)
			            }
					})
				}
			},
	  	}
	},
	components: {
	  	Upload,
	  	ScreenShot
	},
	methods: {
	  	/**
	     * 获取外观图URL
	     */
	  	onGetOutsideUrl(value) {
	  		this.basicForm.coverPhotoUrl = value;
	  	},
	  	/**
	     * 获取内部图URL
	     */
	  	onGetinsideUrl(value) {
	  		this.basicForm.detailPhotoUrl = value;
	  	},
	  	/**
	     * 搜索地址
	     */
		onSearchResult(pois) {
			let latSum = 0,
				lngSum = 0,
				len = pois.length;
			if(len) {
				pois.forEach(poi => {
					let {lng, lat} = poi;
					latSum += lat;
					lngSum += lng;
				});
				let center = {
					lng: lngSum / len,
					lat: latSum / len
				}

				this.markers[0] = [center.lng, center.lat];
				this.center = [center.lng, center.lat];
				this.basicForm.longitude = center.lng;
				this.basicForm.latitude = center.lat;
			}
		},
		/**
	     * 获取水店基本信息
	     */
		getBasicData() {
			this.$$api_manager_getData(null, res => {
				if(res.event === 0) {
					Object.assign(this.basicForm, {
						detailAddress: res.obj.detailAddress,
						latitude: res.obj.latitude,
						longitude: res.obj.longitude,
						coverPhotoUrl: this.isNullUrl(res.obj.coverPhotoUrl),
						detailPhotoUrl: this.isNullUrl(res.obj.detailPhotoUrl),
						address: res.obj.address
					})

					searchDom.val(res.obj.address);
					this.markers[0] = [res.obj.longitude, res.obj.latitude];
					this.center = [res.obj.longitude, res.obj.latitude];
					this.$refs.outsideUrl.changeImgUrl(res.obj.coverPhotoUrl);
					this.$refs.insideUrl.changeImgUrl(res.obj.detailPhotoUrl);
				}else {
					cbs.submitError.call(this, `获取基本信息失败`);
				}
			})
		},
		/**
	     * 判断图片路径是否为空
	     */
		isNullUrl(url) {
			if(/null$/.test(url)) {
				return '';
			}else {
				return url;
			}
		},
		/**
	     * 提交
	     */
		onPost(name) {
			this.$refs[name].validate(valid => {
				if(valid) {
					this.basicForm.address = searchDom.val();

					if(!this.basicForm.address) {
						cbs.submitError.call(this, `请填写地址`);
						return false;
					}

					let data = this.$$formData(this.basicForm);

					this.$$api_manager_editData(data, res => {
						if(res.event === 0) {
							cbs.submitSuccess.call(this, '修改成功');
							this.$router.push('/index/home');
						}else {
							cbs.submitError.call(this, `提交失败， ${res.msg}`);
						}
					})
				}
			});
		},
		/**
	     * 重置
	     */
		onReset() {
			this.$refs.basicForm.resetFields();
		}
	},
	created() {
	    this.getBasicData();
	},
	mounted() {
	    searchDom = $('.search-box-wrapper input');
	}
}